/**
 * Created by caosh on 2018/1/21 0021.
 */
var Search = new Vue({
    el: '#search',
    data: {
        isShowMask: false,
        product: {},
        num: 1,
        pId: 0,
        isShowCart: false,
        cartBarTip: '编辑',
        searchValue: '',
        historySearchList: [],
        showSearchResult: false,
        count: 0 // 查询次数
    },
    computed: {
    },
    mixins: [CartMixin, ProductMixin],
    methods: {
        goBack: function () {
            if (this.count == 0) {
                window.history.back(-1);
            } else {
                this.count--;
                this.showSearchResult = !this.showSearchResult;
            }
        },
        // 查询
        search: function () {
            if (this.searchValue.length > 0) {
                this.count++;
                this.showSearchResult = !this.showSearchResult;
                this.historySearchList.unshift({value:this.searchValue, isTouch: false});
                store.set('historySearchList', this.historySearchList);
                // 查询数据
                this.getProductsByName(this.searchValue);
            }
        },
        // 清空查询
        clearHistorySearch: function () {
            this.historySearchList = [];
            store.remove('historySearchList');
        },
        // 点击历史搜索进行查询
        clickHistorySearch: function (s) {
            // 从历史搜索中移除
            this.historySearchList.splice(this.historySearchList.indexOf(s), 1);
            this.searchValue = s.value;
            this.search();
        },
        clickPlus: function (p, n) {
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }
            this.changeMask(p, n);
            n.click = !n.click;
        },
        // 隐藏 / 显示 遮罩层
        changeMask: function (p, n) {
            this.isShowMask = !this.isShowMask;
            if (this.isShowMask) {
                this.product = this.getProductFromPool(p, n);
                this.product.unitPrice = n.price;
                this.product.unit = n.unit;
                this.product.nId = n.id;
                this.num = this.product.num;
                this.pId = p.id;
            } else {
                this.pId = 0;
            }
        },
        // 取消
        cancel: function () {
            this.num = this.product.num;
            this.changeMask();
        },
        // 确定
        confirm: function () {
            this.product.num = this.num;
            if (this.cartProducts.indexOf(this.product) == -1) {
                this.cartProducts.push(this.product);
            }
            // 本地缓存购物车数据
            // this.cacheCartProducts();
            this.changeMask();
        },
        // 加
        plus: function () {
            this.num++;
        },
        // 减
        reduce: function () {
            if (this.num > 1) {
                this.num--;
            }
        },
        toShowDetails: function (p) {
            this.showDetails(p);
            window.location.href = '../details/index.html';
        }/*,
        // 去支付
        submitOrder: function () {
            store.set('order', {products: this.cartProducts, total: this.cartTotal});
            window.location.href = '../cart/order.html';
        }*/
    },
    mounted: function () {
        // 获得本地缓存的搜索记录
        this.historySearchList = store.get('historySearchList') || [];
        // 获得购物车数据
        this.getCartProducts();
    },
    watch: {
        num: function (val, oldVal) {
            if (val < 1) {
                this.num = 1;
            }
        }
    }
});